package com.servlet;

import com.alibaba.fastjson.JSON;
import com.dao.*;
import com.dao.Impl.*;
import com.entity.*;
import com.util.MyUtil;
import com.util.TransBean;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@WebServlet(value = "/rechargeruleServlet/*")
public class RechargeruleServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String uri = request.getRequestURI();
        String method = uri.substring(uri.lastIndexOf("/") + 1);
        String target = null;
        switch (method){
            case "findByPage":
                target = findByPage(request, response);
                break;
            case "insert":
                target = insert(request, response);
                break;
            case "delete":
                target = delete(request,response);
                break;
            case "findname":
                target = findname(request, response);
                break;
            case "modify":
                target = modify(request, response);
                break;
            case "findId":
                target = findId(request, response);
                break;
            case "findAllRule":
                target = findAllRule(request, response);
                break;
            case "insertRule":
                target = insertRule(request, response);
                break;
            case "updateRule":
                target = updateRule(request, response);
                break;
            default:break;
        }
        if (target != null){
            request.getRequestDispatcher(target).forward(request,response);
        }
    }

    /**
     * 充值规则修改
     * @param request
     * @param response
     * @return
     */
    private String updateRule(HttpServletRequest request, HttpServletResponse response) {
        Rechargerule rrule=new Rechargerule();
        TransBean.populate(rrule,request.getParameterMap());
        RechargeruleDao rdao=new RechargeruleDaoImpl();
        rdao.updateRule(rrule);
        return null;
    }

    // 好难啊，我真的不会，还是请教俺壮哥才做出来的，
// 但是我其实还是不会，嘻嘻
    private String findByPage(HttpServletRequest request,HttpServletResponse response){
        PageBean pageBean = new PageBean();
        PrintWriter out=null;
        try {
             out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        TransBean.populate(pageBean, request.getParameterMap());
        RechargeruleDao rechargeruleDao = new RechargeruleDaoImpl();
        rechargeruleDao.findByPage(pageBean);
        String jsonStr = JSON.toJSONString(pageBean);
        out.print(jsonStr);
        out.close();
        return null;
    }

    /**
     *
     * @param request
     * @param response
     *
     * 普通的添加的方法
     *
     */
    private String insert(HttpServletRequest request,HttpServletResponse response) throws  IOException{
        System.out.println("123");
        Rechargerule rechargerule = new Rechargerule();
        RechargeruleDao rechargeruleDao = new RechargeruleDaoImpl();
        TransBean.populate(rechargerule, request.getParameterMap());
        rechargeruleDao.insert(rechargerule);
        System.out.println(rechargerule);
        response.sendRedirect("/rechargeruleServlet/findByPage");
        return null;
    }

    /**
     *
     * @param request
     * @param response
     * @return
     * 通过name 来做 模糊查询
     * 现在还用不到，就没写
     * 阿杰说模糊查询分页里面有
     */
    private String findname(HttpServletRequest request, HttpServletResponse response){
        return "";
    }

    /**
     *
     * @param request
     * @param response
     * @return
     *
     * 普通的删除方法
     * 用不到了
     */
    private String delete(HttpServletRequest request, HttpServletResponse response){
        RechargeruleDao rechargeruleDao = new RechargeruleDaoImpl();
        int id= Integer.parseInt(request.getParameter("id"));
        rechargeruleDao.delete(id);
        return "/top_up_rule/top_up_rules.jsp";
    }
    /**
     * 普通的修改方法，
     *
     */
    private String modify(HttpServletRequest request, HttpServletResponse response){
        Map<String, String[]> map = request.getParameterMap();
        Rechargerule rechargerule = new Rechargerule();
        try {
            BeanUtils.populate(rechargerule, map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        RechargeruleDao rechargeruleDao = new RechargeruleDaoImpl();
        rechargeruleDao.modify(rechargerule);
        return "/rechargeruleServlet/findByPage";
    }

    /**
     *获取到id
     *普通的获取id
     */
    private String findId(HttpServletRequest request, HttpServletResponse response) {
        Integer id = Integer.valueOf(request.getParameter("id"));
        RechargeruleDao dao = new RechargeruleDaoImpl();
        Rechargerule cate = dao.findById(id);
        MyUtil.jsonWriter(response, cate);
        return null;
    }


    /**
     * 分页查询所有充值规则
     * @param request
     * @param response
     * @return
     */
    private String findAllRule(HttpServletRequest request, HttpServletResponse response) {
        PageBean<Map> page=new PageBean<>();
        TransBean.populate(page,request.getParameterMap());
        RechargeruleDao dao=new RechargeruleDaoImpl();
        dao.findAllRule(page);
        MyUtil.jsonWriter(response,page);
        return null;
    }

    /**
     * 新增规则
     * @param request
     * @param response
     * @return
     */
    private String insertRule(HttpServletRequest request, HttpServletResponse response) {
        Rechargerule rrule=new Rechargerule();
        TransBean.populate(rrule,request.getParameterMap());
        RechargeruleDao rdao=new RechargeruleDaoImpl();
        rdao.insertRule(rrule);
        return null;
    }



}
